package com.st.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.st.entity.Result;
import com.st.entity.User;
import com.st.entity.UserDto;
import com.st.entity.UserInfo;
import com.st.mapper.IRegisterMapper;
import com.st.mapper.IUserInfoMapper;
import com.st.service.IRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import util.ImageCodeUtil;
import util.SendMessageUtil;
import util.UserInfoUtil;
import util.VerifyCodeUtil;

import java.awt.image.BufferedImage;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Service
@Transactional
public class RegisterServiceImpl extends ServiceImpl<IRegisterMapper,User> implements IRegisterService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private IRegisterMapper iRegisterMapper;
    @Autowired
    private IUserInfoMapper iUserInfoMapper;
    //获取图形验证码
    @Override
    public Result pigCode() {
        BufferedImage image = ImageCodeUtil.getBufferedImage();
        String code = ImageCodeUtil.getCodeNum();
//        String code = VerifyCodeUtil.generateVerifyCode(4, "1234567890");
        stringRedisTemplate.boundValueOps("code_register").set(code);
        stringRedisTemplate.expire("code_register",5,TimeUnit.MINUTES);
        return new Result(true,"生成验证码",image);
    }
    //发送手机验证码
    @Override
    public Result sendMsgCode(String phone) {
        if (StringUtils.isEmpty(phone)) {
            return new Result(false,"手机号为空");
        }
        String code = VerifyCodeUtil.generateVerifyCode(4, "abcdqwertyuiop8912345672wqwe");
        boolean b = SendMessageUtil.sendMsg(phone, code);
        if (b){
            String key = phone+"_register";
            stringRedisTemplate.boundValueOps(key).set(code);
            stringRedisTemplate.expire(key, 30, TimeUnit.MINUTES);
            return new Result(true,"发送成功",code);
        }
        return new Result(true,"发送失败");
    }

    //用户注册
    @Override
    @Transactional
    public Result register(UserDto userDto) {
        if (userDto == null) {
            return new Result(false,"参数非法");
        }
        String pigCode = userDto.getPigCode();
        String code_register = stringRedisTemplate.boundValueOps("code_register").get();
        if (!pigCode.equalsIgnoreCase(code_register)){
            return new Result(false,"图形验证码错误");
        }
        String msgCode = userDto.getMsgCode();
        String account = userDto.getAccount();
        String msgCode_register = stringRedisTemplate.boundValueOps(account + "_register").get();
        if (!msgCode.equalsIgnoreCase(msgCode_register)){
            return new Result(false,"手机验证码错误");
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_mobile", userDto.getAccount());
        UserInfo userInfo = iUserInfoMapper.selectOne(queryWrapper);
        if (userInfo != null) {
            return new Result(false,"手机号已注册");
        }
        String userType = userDto.getUserType();
        if ("2".equals(userType)){
            userDto.setUserDescribe("普通用户");
        }else {
            userDto.setUserDescribe("管理员");
        }
//        userDto.setUserType(2+"");
        userDto.setUserStatus(1);
        String stringRandom = UserInfoUtil.getStringRandom(8);
        userDto.setAccountSql(stringRandom);
        userDto.setUserTime(new Date());
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(userDto.getPassword());
        userDto.setPassword(encode);
        int i = iRegisterMapper.registerUserInfo(userDto);
        int i1 = iRegisterMapper.registerUser(userDto);
        return new Result(true,"注册成功");
    }
}
